package com.rookie.tool.module.util.html;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author rookie
 */
@Slf4j
public class ExtractHtmlUtils {

    /**
     * ip+端口号
     */
    public static List<String> ipList;

    /**
     * 是否开启代理
     */
    public static boolean proxyStatus = false;

    private static final String IP_SPLIT = ":";

    private static int point = 0;

    public static Document getHtmlContent(String url) {
        try {
            return Jsoup.connect(url).get();
        } catch (Exception e) {
            if (proxyStatus) {
//                开启全局代理
                startProxy();
                nextIp();
            }
        }
        return null;
    }

    /**
     * 检测IP的有效性
     *
     * @param ip ip+端口
     * @return 结果
     */
    private static boolean checkIp(String ip) {
        int ipSection = 2;
        if (!StringUtils.hasText(ip)) {
            return false;
        }
        return ip.split(IP_SPLIT).length == ipSection;
    }

    private static void nextIp() {
        if (ipList != null && ipList.size() > 0 && point < ipList.size()) {
            String ip = ipList.get(point);
            if (checkIp(ip)) {
                String[] ipDetail = ip.split(IP_SPLIT);
                switchIp(ipDetail[0], ipDetail[1]);
            } else {
                log.debug(String.format("ip'%s'", ip) + "异常");
            }
            point++;
        } else {
            log.debug("代理ip已空，请填充代理ip");
//            关闭代理
            stopProxy();
        }
    }

    /**
     * 全局开启代理
     */
    public static void startProxy() {
//        开启状态
        String ensureValue = "true";
        String proxy = System.getProperties().getProperty("proxySet");
        if (ensureValue.equals(proxy)) {
            return;
        }
        System.getProperties().setProperty("proxySet", "true");
    }


    /**
     * 关闭全局代理
     */
    public static void stopProxy() {
        String ensureValue = "false";
        String proxy = System.getProperties().getProperty("proxySet");
        if (ensureValue.equals(proxy)) {
            return;
        }
        System.getProperties().setProperty("proxySet", "false");
    }

    /**
     * 切换代理IP
     */
    public static void switchIp(String ip, String port) {
        System.getProperties().setProperty("http.proxyHost", ip);
        System.getProperties().setProperty("http.proxyPort", port);
    }

    public static Document getHtml(String url) {
        System.setProperty("webdriver.chrome.driver", "e:/chromedriver.exe");
        WebDriver wd = new ChromeDriver();
        wd.get(url);
        try {
            Thread.sleep(1200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Jsoup.parse(wd.getPageSource());
    }

    private static void driverTest() {
        System.setProperty("webdriver.chrome.driver", "e:/chromedriver.exe");
        WebDriver wd = new ChromeDriver();
        JavascriptExecutor executor = (JavascriptExecutor) wd;
//        执行js脚本
        executor.executeScript("");
    }

}
